package com.view.adminMenu;

import com.run.BookRun;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Objects;

public class serchBookMenu {
    private JPanel mainPanel = new JPanel();
    private JLabel book_idLabel = new JLabel("图书编号:");
    private JLabel book_nameLabel = new JLabel("图书名称:");
    private JTextField book_idField = new JTextField(20);
    private JTextField book_nameField = new JTextField(20);
    private ScrollPane mainPane = new ScrollPane();
    private TextArea mainArea = new TextArea(100,100);
    private Box searchBox = Box.createHorizontalBox();
    private Box mainBox = Box.createVerticalBox();
    private JButton searchButton = new JButton("搜索");

    public JPanel init() {
        //mainPane.add(mainArea);

        searchBox.add(Box.createHorizontalStrut(20));
        searchBox.add(book_idLabel);
        searchBox.add(Box.createHorizontalStrut(20));
        searchBox.add(book_idField);
        searchBox.add(Box.createHorizontalStrut(40));
        searchBox.add(book_nameLabel);
        searchBox.add(Box.createHorizontalStrut(20));
        searchBox.add(book_nameField);
        searchBox.add(Box.createHorizontalStrut(40));
        searchBox.add(searchButton);

        mainBox.add(searchBox);
        mainBox.add(mainArea);

        mainPanel.add(mainBox);

        mainArea.append("\n");
        mainArea.append("图书编号          ");
        mainArea.append("图书名称          ");
        mainArea.append("拥有数量          ");
        mainArea.append("借出次数          ");
        mainArea.append("备注             ");
        mainArea.append("\n");

        searchButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String book_id = book_idField.getText();
                String book_name = book_nameField.getText();
                Connection conn = null;
                try {
                    conn = com.util.DBUtil.getConnection();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                if(Objects.equals(book_id,"")) {
                    try {
                        ResultSet resultSet = new BookRun().searchBook(conn,null,book_name);
                        while(resultSet.next()) {
                            book_id = resultSet.getString(1);
                        }

                    } catch (SQLException ex) {
                        ex.printStackTrace();
                    }
                }
                try {
                    ResultSet resultSet = new BookRun().SEARCHBOOKS(conn,book_id,book_name);
//                    System.out.printf("%-12s","图书编号");
//                    System.out.printf("%-12s","图书名称");
//                    System.out.printf("%-14s","拥有数量");
//                    System.out.printf("%-14s","借出次数");
//                    System.out.printf("%-15s","备注");
//                    System.out.println();

                    boolean isNull = true;
                    while(resultSet.next()){
                        System.out.printf("%-15s",resultSet.getString(1));
                        System.out.printf("%-15s",resultSet.getString(2));
                        System.out.printf("%-15s",resultSet.getInt(3));
                        System.out.printf("%-15s",resultSet.getInt(4));
                        System.out.printf("%-15s",resultSet.getString(5));
                        System.out.println();
                        mainArea.append("\n");
                        mainArea.append(resultSet.getString(1)+"                    ");
                        mainArea.append(resultSet.getString(2)+"                    ");
                        mainArea.append(resultSet.getInt(3)+"                    ");
                        mainArea.append(resultSet.getInt(4)+"                    ");
                        mainArea.append(resultSet.getString(5)+"                    ");
                        mainArea.append("\n");
                        isNull = false;
                    }
                    if(isNull == true) {
                        JOptionPane.showMessageDialog(null,"未查询到此书籍,请查正编号或书名");
                    }

                } catch (SQLException ex) {
                    ex.printStackTrace();
                }

            }
        });

        return mainPanel;
    }


}
